import { Component } from 'vue'
import Page from './Page'
import { Custom } from 'custom/index'

export default class Menu {
  public name: string = '';
  private _pages: Map<string, Page> = new Map();

  add(page: Page) {
    const name = page.name
    if (!name) return
    this._pages.set(name, page)
    if (!this.name) {
      this.name = name
    }
    return this
  }

  addPage<T extends Custom>(custom: T, name = '', path = '', tag = '') {
    const page = new Page()
    page.component(custom.getComponent())
    page.name = name
    page.path = path
    page.tag = tag
    return this.add(page)
  }

  addCustomPage(component: Component, name = '', path = '', tag = '') {
    const page = new Page()
    page.component(component)
    page.name = name
    page.path = path
    page.tag = tag
    return this.add(page)
  }

  eachPage(cb: (page: Page, key: string) => void) {
    this._pages.forEach(cb)
  }
}
